﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany
{
    public partial class main : Form
    {
        public eCompany.eObj.organisation m_org = null;
        public eCompany.eObj.currency_list m_cl = null;
        public eCompany.eManager.eManager m_eM = null;

        private eCompany.Forms.splashForm m_splash = null;

#if DEBUG
        public string companyFilename = @"company.xml";
        public string currenciesFilename = @"currency.xml";
#else
        public string companyFilename = @"company.xml";
        public string currenciesFilename = @"currency.xml";
#endif

        public main()
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;
        }

        public void InitMenu()
        {
            companiesToolStripMenuItem.DropDownItems.Clear();

            ToolStripMenuItem org_item = new ToolStripMenuItem();
            org_item.Text = m_org.name;
            org_item.Tag = m_org.id;
            org_item.Click += new EventHandler(org_item_Click);
            companiesToolStripMenuItem.DropDownItems.Add(org_item);


            if (m_org.company_list.Count > 0)
            {
                ToolStripSeparator _separator = new ToolStripSeparator();
                companiesToolStripMenuItem.DropDownItems.Add(_separator);
            }
                        
            foreach (eCompany.eObj.company c in m_org.company_list)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = c.name;
                item.Tag = c.id;
                item.Click += new EventHandler(companyToolStripMenuItem_Click);
                item.Image = Tools.GetCompanyTypeImage(c.type);
                companiesToolStripMenuItem.DropDownItems.Add(item);                
            }
            ToolStripSeparator separator = new ToolStripSeparator();
            companiesToolStripMenuItem.DropDownItems.Add(separator);

            //ToolStripMenuItem add_company_item = new ToolStripMenuItem();
            //add_company_item.Text = "Add new company";
            //add_company_item.Click += new EventHandler(addCompanyToolStripMenuItem_Click);
            //companiesToolStripMenuItem.DropDownItems.Add(add_company_item);

            ToolStripMenuItem add_api_company_item = new ToolStripMenuItem();
            add_api_company_item.Text = "Add new company from eAPI";
            add_api_company_item.Click += new EventHandler(addApiCompanyToolStripMenuItem_Click);
            companiesToolStripMenuItem.DropDownItems.Add(add_api_company_item);
        }

        private void addCompanyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNewCompany();
        }

        private void addApiCompanyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNewAPICompany();
        }
        
        public void AddNewCompany()
        {
            eCompany.Forms.add_company_form f = new Forms.add_company_form(ref m_org);
            f.MdiParent = this;
            f.Show();
        }

        public void AddNewAPICompany()
        {
            eCompany.Forms.api_add_company f = new Forms.api_add_company(ref m_org);
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                InitMenu();
            }
        }

        private void org_item_Click(object sender, EventArgs e)
        {
            eCompany.Forms.organisation_detail_form f = new eCompany.Forms.organisation_detail_form(m_org, m_cl);
            f.MdiParent = this;
            f.Show();
        }
        private void companyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem i = (ToolStripMenuItem)sender;
            string id = (string)i.Tag;
            eCompany.eObj.company c = m_eM.getCompany(m_org, Guid.Parse(id));

            eCompany.Forms.company_form f = new eCompany.Forms.company_form(c, m_cl);
            f.MdiParent = this;
            f.Show();
        }

        private void currenciesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.Forms.currencies_form f = new eCompany.Forms.currencies_form(ref m_cl);
            f.MdiParent = this;
            f.Show();
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void main_FormClosed(object sender, FormClosedEventArgs e)
        {
            if(m_org != null)
                m_eM.saveFile(companyFilename, m_org);

            if(m_cl != null)
                m_eM.saveFile(currenciesFilename, m_cl);
        }

        private void reportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.Forms.report r = new eCompany.Forms.report(m_org, m_cl);
            r.MdiParent = this;
            r.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.Forms.eCompanyAboutBox dlg = new eCompany.Forms.eCompanyAboutBox();
            dlg.ShowDialog();
        }

        private void main_Load(object sender, EventArgs e)
        {
            m_splash = new eCompany.Forms.splashForm();
            m_splash.Show();

            m_eM = new eManager.eManager();
            try
            {
                m_splash.setLoadingLabel("Loading organisation file");
                m_splash.updateLoadingLabel();

                m_eM.loadFile(companyFilename, ref m_org);
                m_splash.setLoadingLabel("Patching");
                m_splash.updateLoadingLabel();
                try
                {
                    if (eCompany.Patchs.patchs.Patchs(m_org, m_splash.updateLoadingLabel) == false)
                    {
                        m_org = null;
                        m_splash.Close();
                        m_splash.Dispose();
                        m_splash = null;

                        Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot patch organisation.\n" + ex.Message, "Patching Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    m_org = null;
                    m_splash.Close();
                    m_splash.Dispose();
                    m_splash = null;

                    Close();
                }
            }
            catch (System.IO.FileNotFoundException ex)
            {
                //Need to launch wizard
                m_org = null;
                m_splash.Visible = false;

                string corpname = "";
                string user_id = "";
                if (Tools.InputBox("Create new corp", "Corp Name", "User api Id", ref corpname, ref user_id) == System.Windows.Forms.DialogResult.OK)
                {
                    m_org = new eObj.organisation();
                    m_org.name = corpname;
                    m_org.id = Guid.NewGuid().ToString();
                    m_org.api_id = user_id;
                    eObj.account acc = new eObj.account();
                    acc.id = Guid.NewGuid().ToString();
                    acc.currency = "GLD";
                    m_org.account_list.Add(acc);
                }
                else
                {
                    m_splash.Close();
                    m_splash.Dispose();
                    m_splash = null;

                    this.Close();
                    return;
                }
                m_splash.Visible = true;
            }

            try
            {
                m_eM.loadFile(currenciesFilename, ref m_cl);
            }
            catch (System.IO.FileNotFoundException ex)
            {
                //Need to load currencies
                m_cl = new eObj.currency_list();
                m_splash.setLoadingLabel("No existing currency file. Loading currency from erep api");
                m_eM.refreshCurrencies(ref m_cl, m_splash.updateLoadingLabel);
            }
#if DEBUG
            this.Text = "eCompany " + Application.ProductVersion.ToString()+" Debug";
#else
            this.Text = "eCompany " + Application.ProductVersion.ToString();
#endif

            InitMenu();
            System.Threading.Thread.Sleep(1000);

            eCompany.Forms.organisation_form f = new eCompany.Forms.organisation_form(ref m_org, ref m_cl);
            f.MdiParent = this;
            f.Show();

            m_splash.Close();
            m_splash.Dispose();
            m_splash = null;
        }

        private void calcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.Forms.currency_calc_form f = new Forms.currency_calc_form(m_cl, "FRF");
            f.MdiParent = this;
            f.Show();
        }
    }
}
